package com.example.demotest.exception;


import com.example.demotest.pojo.ResponseMessage;
import com.sun.net.httpserver.HttpServer;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandlerAdvice<T> {

    Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);

    @ExceptionHandler(UsernameAlreadyExistsException.class)
    public ResponseMessage<?> handleUsernameExists(UsernameAlreadyExistsException e) {
        return new ResponseMessage<>(409, e.getMessage(), null, null); // 409 Conflict
    }

    @ExceptionHandler(NicknameAlreadyExistsException.class)
    public ResponseMessage<?> handleNicknameExists(NicknameAlreadyExistsException e) {
        return new ResponseMessage<>(409, e.getMessage(), null, null); // 409 Conflict
    }

    @ExceptionHandler({Exception.class})
    public ResponseMessage handlerException(Exception e, HttpServletRequest request, HttpServletResponse response){
        //查日志
        log.error("统一异常: ", e);
        return new ResponseMessage(500, "error", null, null);
    }




}
